package com.ruoyi.system.wnjk.service.impl;

import java.util.ArrayList;
import java.util.List;


import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.system.wnjk.domain.po.ShopOrder;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.wnjk.domain.po.ShopOrderDetail;
import com.ruoyi.system.wnjk.domain.vo.ShopOrderVo;
import com.ruoyi.system.wnjk.mapper.ShopOrderDetailMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.wnjk.mapper.ShopOrderMapper;
import com.ruoyi.system.wnjk.service.IShopOrderService;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-05
 */
@Service
public class ShopOrderServiceImpl implements IShopOrderService
{
    @Autowired
    private ShopOrderMapper shopOrderMapper;

    @Autowired
    private ShopOrderDetailMapper shopOrderDetailMapper;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public ShopOrder selectShopOrderById(Long id)
    {
        return shopOrderMapper.selectShopOrderById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param shopOrder 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<ShopOrder> selectShopOrderList(ShopOrder shopOrder)
    {
        return shopOrderMapper.selectShopOrderList(shopOrder);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param shopOrder 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertShopOrder(ShopOrder shopOrder)
    {
        shopOrder.setCreateTime(DateUtils.getNowDate());
        return shopOrderMapper.insertShopOrder(shopOrder);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param shopOrder 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateShopOrder(ShopOrder shopOrder)
    {
        shopOrder.setUpdateTime(DateUtils.getNowDate());
        return shopOrderMapper.updateShopOrder(shopOrder);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteShopOrderByIds(Long[] ids)
    {
        return shopOrderMapper.deleteShopOrderByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteShopOrderById(Long id)
    {
        return shopOrderMapper.deleteShopOrderById(id);
    }

    @Override
    public List<ShopOrderVo> selectShopOrderListByUser(ShopOrder shopOrder) {
        List<ShopOrder> shopOrders = shopOrderMapper.selectShopOrderList(shopOrder);
        List<ShopOrderVo> result = BeanUtil.copyToList(shopOrders, ShopOrderVo.class);
        ArrayList<ShopOrderDetail> shopOrderDetails = new ArrayList<>();
        ShopOrderDetail shopOrderDetailQuery = new ShopOrderDetail();
        for (ShopOrderVo shopOrderVo : result) {
            shopOrderDetailQuery.setOrderId(shopOrderVo.getId());
            List<ShopOrderDetail> shopOrderDetailsTempList = shopOrderDetailMapper.selectShopOrderDetailList(shopOrderDetailQuery);
            shopOrderVo.setShopOrderDetailList(shopOrderDetailsTempList);
        }
        return result;
    }

    @Override
    public List<ShopOrder> selectUnpaidOrders() {
        // 查询状态为1（未支付）的订单
        return shopOrderMapper.selectUnpaidOrders();
    }

    @Override
    public List<ShopOrderDetail> selectOrderDetailsByOrderId(Long orderId) {
        // 根据订单ID查询订单详情
        return shopOrderDetailMapper.selectByOrderId(orderId);
    }
}
